<template>
  <h2>shallowReactive 与 shallowRef</h2>
  <hr />
  <h3>m1:{{ m1 }}</h3>
  <h3>m2:{{ m2 }}</h3>
  <h3>m3:{{ m3 }}</h3>
  <h3>m4:{{ m4 }}</h3>
  <button @click="update">更新数据</button>
</template>
<script lang="ts">
import {
  defineComponent,
  reactive,
  ref,
  shallowReactive,
  shallowRef,
} from "vue";
export default defineComponent({
  name: "App",
  setup() {
    /* 
    shallowReactive与shallowRef
    shallowReactive: 只处理了对象内最外层属性的响应式(也就是浅响应式)
    shallowRef: 只处理了value的响应式, 不进行对象的reactive处理
    总结:
    reactive与ref实现的是深度响应式, 而shallowReactive与shallowRef是浅响应式
     什么时候用浅响应式呢?
     一般情况下使用ref和reactive即可,
     如果有一个对象数据, 结构比较深, 但变化时只是外层属性变化 ===> shallowReactive
     如果有一个对象数据, 后面会产生新的对象来替换 ===> shallowRef
   */
    const m1 = reactive({
      name: "鸣人",
      age: 10,
      car: {
        name: "九尾小虎",
        age: 10,
      },
    });
    const m2 = shallowReactive({
      name: "鸣人",
      age: 10,
      car: {
        name: "九尾小虎",
        age: 10,
      },
    });
    // value 为代理对象
    const m3 = ref({
      name: "鸣人",
      age: 10,
      car: {
        name: "九尾小虎",
        age: 10,
      },
    });
    // value 为object对象
    const m4 = shallowRef({
      name: "鸣人",
      age: 10,
      car: {
        name: "九尾小虎",
        age: 10,
      },
    });
    console.log(m3, m4);
    const update = () => {
      // reactive
      // m1.car.name += "==";
      // shallowReactive
      // m2.car.name += "==";
      // ref
      m3.value.name += "==";
      // shallowRef
      // m4.value.car.name += "==";
    };
    return {
      m1,
      m2,
      m3,
      m4,
      update,
    };
  },
});
</script>

<style>
</style>
